package com.ruantong.dingtalk.ding.openapi;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.ruantong.dingtalk.ding.constant.CallbackConstant;
import com.ruantong.dingtalk.ding.exception.DingRequestException;
import com.ruantong.dingtalk.ding.openapi.token.DingTokenStore;
import com.ruantong.dingtalk.ding.util.PreCheckResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description:钉钉注册回调事件
 * @author: jh.chen
 * @time: 2020/11/15 13:24
 */
@Component
@Slf4j
public class CallbackApi {

    @Autowired
    private DingTokenStore dingTokenStore;

    public void registerCallBack(List<String> callBackTag, String url) throws DingRequestException {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/register_call_back");
            OapiCallBackRegisterCallBackRequest req = new OapiCallBackRegisterCallBackRequest();
            req.setCallBackTag(callBackTag);
            req.setToken(CallbackConstant.CALL_TOKEN);
            req.setAesKey(CallbackConstant.AES_KEY);
            req.setUrl(url);
            OapiCallBackRegisterCallBackResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("CallBackRegisterCallBack:{}", JSON.toJSONString(rsp));
            PreCheckResponse.isSuccess(rsp);
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
    }

    public void updateCallBack(List<String> callBackTag, String url) throws DingRequestException {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/update_call_back");
            OapiCallBackUpdateCallBackRequest req = new OapiCallBackUpdateCallBackRequest();
            req.setCallBackTag(callBackTag);
            req.setToken(CallbackConstant.CALL_TOKEN);
            req.setAesKey(CallbackConstant.AES_KEY);
            req.setUrl(url);
            OapiCallBackUpdateCallBackResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("CallBackRegisterCallBack:{}", JSON.toJSONString(rsp));
            PreCheckResponse.isSuccess(rsp);
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
    }

    public void getCallBack() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/get_call_back");
            OapiCallBackGetCallBackRequest req = new OapiCallBackGetCallBackRequest();
            req.setHttpMethod("GET");
            OapiCallBackGetCallBackResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("CallBackRegisterCallBack:{}", JSON.toJSONString(rsp));
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
    }

    public void getCallBackFailedResult() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/get_call_back_failed_result");
            OapiCallBackGetCallBackFailedResultRequest req = new OapiCallBackGetCallBackFailedResultRequest();
            req.setHttpMethod("GET");
            OapiCallBackGetCallBackFailedResultResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("CallBackRegisterCallBack:{}", JSON.toJSONString(rsp));
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
    }

    public void deleteCallBack() throws DingRequestException {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/delete_call_back");
            OapiCallBackDeleteCallBackRequest req = new OapiCallBackDeleteCallBackRequest();
            req.setHttpMethod("GET");
            OapiCallBackDeleteCallBackResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("CallBackRegisterCallBack:{}", JSON.toJSONString(rsp));
            PreCheckResponse.isSuccess(rsp);
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
    }
}
